package com.wwcom.darteagle.data;

import java.util.ArrayList;
import java.util.List;

import com.wwcom.darteagle.model.Player;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;

public class PlayerObject extends DataObject {
    private static final String TABLE_NAME = "PLAYERS";
    private static final String TABLE_CREATE =
            "CREATE TABLE " + TABLE_NAME + " (" +
            "PLAYER_ID integer primary key autoincrement," +
            "PLAYER_NAME text);";
    
    public PlayerObject(SQLiteDatabase database) {
    	super(database);
    }
    
	static public String createSQL() {
		return TABLE_CREATE;
	}

    static public String upgradeSQL(int oldVersion, int newVersion) {
    	return null;
    }

	static public String deleteSQL() {
		return "DROP TABLE IF EXISTS "+TABLE_NAME;
	}

	public Player get(long playerId) {
    	Cursor cursor = database.query(TABLE_NAME, new String[] {"PLAYER_ID","PLAYER_NAME"}, 
    			"PLAYER_ID="+playerId, null, null, null, null);
    	if(cursor.getCount() != 1)
    		throw new RuntimeException("PlayerObject::get - non-unique player specified: "+cursor.getCount());
        cursor.moveToNext();
        Player p= new Player(cursor.getString(cursor.getColumnIndex("PLAYER_NAME")));
        p.setId(cursor.getLong(cursor.getColumnIndex("PLAYER_ID")));
        return p;
    }

	public Player get(String playerName) {
    	Cursor cursor = database.query(TABLE_NAME, new String[] {"PLAYER_ID","PLAYER_NAME"}, 
    			"PLAYER_NAME="+DatabaseUtils.sqlEscapeString(playerName), null, null, null, null);
    	if(cursor.getCount() != 1)
    		throw new RuntimeException("PlayerObject::get - non-unique player specified");
        cursor.moveToNext();
        Player p= new Player(cursor.getString(cursor.getColumnIndex("PLAYER_NAME")));
        p.setId(cursor.getLong(cursor.getColumnIndex("PLAYER_ID")));
        return p;
    }
    
    public void add(Player player) {
    	Cursor cursor = database.query(TABLE_NAME, new String[] {"PLAYER_ID"}, 
    			"PLAYER_NAME = "+DatabaseUtils.sqlEscapeString(player.getName()), null, null, null, null);
    	if (cursor.getCount() == 0) {
    		insert(player);  //insert player
    	} else {
            cursor.moveToNext();  //otherwise just make sure the correct player id is set
            player.setId(cursor.getInt(cursor.getColumnIndex("PLAYER_ID")));    		
    	}
    }
    
    private void insert(Player player)
    {
        ContentValues contentValues = new ContentValues();
        contentValues.put("PLAYER_NAME", escapeNoQuotes(player.getName()));
        long playerId= database.insert(TABLE_NAME, null, contentValues);
        player.setId(playerId);
    }    
    
    public List<Player> getPlayers() {
    	Cursor cursor = database.query(TABLE_NAME, new String[] {"PLAYER_NAME"}, null, null, null, null, null);    	 
    	List<Player> players= new ArrayList<Player>(cursor.getCount());
        if(cursor.getCount() >0) {
            while (cursor.moveToNext()) {
                players.add(get(cursor.getString(cursor.getColumnIndex("PLAYER_NAME"))));
             }
        }
        return players;
    }
               
}
